/**
 * @param {number} target
 * @param {number[]} nums
 * @return {number}
 */
// const minSubArrayLen = function(target, nums) {
//     let slowKey = 0, fastKey = 0, len = 0;
//     while (slowKey < nums.length) {
//         let val = 0
//         for(let i = slowKey; i<= fastKey; i++) {
//             val += nums[i]
//         }
//         if(val >= target) {
//             const newLen = fastKey - slowKey + 1
//             len = len > newLen || len === 0? newLen : len
//             slowKey++
//         } else if(fastKey < nums.length - 1){
//             fastKey++
//         } else {
//             break
//         }
//     }
//     return len
// };
// 这个方法因为 for循环计算和，导致时间复杂度比较长
const minSubArrayLen = function(target, nums) {
    let slowKey = 0, fastKey = 0, len = 0, sumVal = 0;
    while (fastKey < nums.length) {
        sumVal += nums[fastKey]
        while(sumVal >= target) {
            const newLen = fastKey - slowKey + 1
            len = len > newLen || len === 0? newLen : len
            sumVal -= nums[slowKey++]
        }
        fastKey++
    }
    return len
};
const target = 11, nums = [1,1,1,1,1,1,1,1]
console.log(minSubArrayLen(target, nums))